<?php

/***
 * Cogsworth
 *
 * An open source multi-application MVC-based framework written in PHP with XML and XSL in mind.
 *
 * @package Cogsworth
 * @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
 * @copyright Copyright (c) 2008, Daniel Wilhelm II Murdoch
 * @link http://www.getcogsworth.com
 * @since Version 1.0
 * @filesource
 ***/

/***
 * CW_BenchmarkClass
 *
 * A static class that allows you to monitor elapsed time within profile points.
 *
 * @package Cogsworth
 * @category Core Library
 * @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
 * @copyright Copyright (c) 2008, Daniel Wilhelm II Murdoch
 * @link http://www.getcogsworth.com
 * @since Version 1.0
 ***/
class CW_BenchmarkClass {

   /**
	* Contains benchmarking profiles
	* @access Private
	* @var Array
	*/
	static private $profiles = array();


   // ! Executor Method

   /**
	* Creates and begins a precision timer profile
	*
	* @param String $profile A key-friendly profile title for referencing purposes
	* @param String $description An optional description for debuggin
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Float
	*/
	static public function start($profile, $description = '')
	{
		if($description)
		{
			self::$profiles[$profile]['description'] = $description;
		}

		$microtime = explode(' ', microtime());

		self::$profiles[$profile]['start'] = $microtime[1] + $microtime[0];

		return self::$profiles[$profile]['start'];
	}


   // ! Executor Method

   /**
	* Ends a precision timer profile and elapsed time
	*
	* @param String $profile A key-friendly profile title for referencing purposes
	* @param Integer $round Optionally returns elapsed time rounded to this decimal value
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Integer
	*/
	public function stop($profile, $round = 4)
	{
		$microtime = explode(' ', microtime());

		self::$profiles[$profile]['stop'] = round(($microtime[1] + $microtime[0]) - self::$profiles[$profile]['start'], $round);

		unset(self::$profiles[$profile]['start']);

		return self::$profiles[$profile]['stop'];
	}


   // ! Accessor Method

   /**
	* Returns elapsed time for a specified profile
	*
	* @param String $profile A key-friendly profile title for referencing purposes
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Mixed
	*/
	public function get($profile)
	{
		return isset(self::$profiles[$profile]) ? self::$profiles[$profile] : false;
	}


   // ! Executor Method

   /**
	* Returns the sum of elsapsed times for a specified set of profiles
	*
	* @param Array $filter Optionally specifies profiles to tally
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Integer
	*/
	public function total($filter = array())
	{
		$total = 0;

		if(false == $filter)
		{
			$filter = array_keys(self::$profiles);
		}

		foreach(self::$profiles as $name => $profile)
		{
			if(in_array($name, $filter))
			{
				$total += $profile['stop'];
			}
		}

		return $total;
	}


   // ! Executor Method

   /**
	* Clears a specified set of profiles or, if no filter is specified,
	* clear them all.
	*
	* @param Array $filter Optionally specifies profiles to tally
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function clear($filter = array())
	{
		if(false == $filter)
		{
			$filter = array_keys(self::$profiles);
		}

		foreach(self::$profiles as $name => $profile)
		{
			if(in_array($name, $filter))
			{
				unset(self::$profiles[$name]);
			}
		}

		return true;
	}
}

?>